#include "FileUtil.h"

FileUtil::FileUtil()
{
}

FileUtil::~FileUtil()
{
}

bool FileUtil::Exists(std::string path)
{
	return boost::filesystem::exists(path);
}

bool FileUtil::IsDir(std::string path)
{
	boost::filesystem::path pt(path);
	return boost::filesystem::is_directory(pt);
}

bool FileUtil::Mkdirs(std::string path)
{
	boost::filesystem::path pt(path);
	return boost::filesystem::create_directories(pt);
}

bool FileUtil::RemoveDirs(std::string dir_path)
{
	// 删除目录-非空目录
	//boost::filesystem::path pt(dir);
	//return boost::filesystem::remove_all(pt) > 0;

	try
	{
		// 目录不存在或非目录
		if (!boost::filesystem::exists(dir_path) ||
			!boost::filesystem::is_directory(dir_path))
			return true;

		// 递归删除所有文件
		for (auto& entry : boost::filesystem::recursive_directory_iterator(dir_path))
		{
			// 跳过目录
			if (boost::filesystem::is_directory(entry.status())) {
				continue;
			}
			try
			{
				// 删除文件
				boost::filesystem::remove(entry.path());
			}
			catch (std::exception& e)
			{
				printf("delete file error:%s", e.what());
			}
		}

		// 递归删除所有文件
		for (auto& entry : boost::filesystem::recursive_directory_iterator(dir_path))
		{
			// 删除空目录目录
			if (boost::filesystem::is_directory(entry.status())) {
				try
				{
					boost::filesystem::remove(entry.path());
				}
				catch (std::exception& e)
				{
					printf("delete dir error:%s", e.what());
				}
			}
		}

		// 删除最外层目录
		boost::filesystem::remove(dir_path);
		return true;
	}
	catch (std::exception& e)
	{
		printf("remove dir error:%s", e.what());
	}
	return false;
}

bool FileUtil::RemoveFile(std::string f)
{
	boost::filesystem::path pt(f);
	return boost::filesystem::remove(pt);
}

std::string FileUtil::GetAppPath(std::string file)
{
	boost::filesystem::path dir = boost::filesystem::current_path();
	boost::filesystem::path result = dir / file;
	return boost::algorithm::replace_all_copy(result.string(), "\\", "/");
}

std::string FileUtil::GetFileName(std::string file)
{
	boost::filesystem::path pt(file);
	return pt.filename().string();
}

unsigned long long FileUtil::GetFileSize(std::string file)
{
	boost::filesystem::path pt(file);
	return boost::filesystem::file_size(pt);
}

void FileUtil::GetCurDirFileList(std::string dir, std::vector<std::string>& list)
{
	boost::filesystem::path pt(dir);
	boost::filesystem::directory_iterator endIter;
	for (boost::filesystem::directory_iterator it(pt); it != endIter; it++)
	{
		if (!boost::filesystem::is_directory(*it))
		{
			list.push_back(it->path().string());
		}
	}
}

void FileUtil::GetRecursiveFileList(std::string dir, std::vector<std::string>& list)
{
	boost::filesystem::path pt(dir);
	boost::filesystem::recursive_directory_iterator endIter;
	for (boost::filesystem::recursive_directory_iterator it(pt); it != endIter; it++)
	{
		if (!boost::filesystem::is_directory(*it))
		{
			list.push_back(it->path().string());
		}
	}
}

bool FileUtil::ReadFile(std::string n, unsigned char* buf, unsigned long long size)
{
	FILE* file = fopen(n.c_str(), "rb");
	if (file)
	{
		fseek(file, 0, SEEK_END);
		unsigned long long length = ftell(file);
		fseek(file, 0, SEEK_SET);
		fread(buf, length, 1, file);
		fclose(file);
		return true;
	}
	return false;
}

std::string FileUtil::ReadText(std::string path)
{
	std::string text;
	std::ifstream file(path);
	std::string line;
	while (std::getline(file, line)) {
		text += line;
	}
	file.close();
	return text;
}

std::string FileUtil::Read(std::string path)
{
	// 二进制打开文件
	std::ifstream file(path.c_str(), std::ios::binary);
	if (!file) {
		return std::string("");
	}

	// 获取文件大小
	file.seekg(0, std::ios::end);
	std::streamsize size = file.tellg();
	file.seekg(0, std::ios::beg);

	// 读取文件内容
	char* content = new char[size + 1];
	memset(content, 0, size + 1);
	if (!file.read(content, size))
	{
		delete[]content;
		return std::string("");
	}
	std::string result(content, size);
	delete[]content;
	return result;
}

bool FileUtil::Write(std::string path, std::string& content)
{
	std::ofstream ofs;
	std::locale loc = std::locale::global(std::locale(""));
	ofs.open(path.c_str(), std::ios::out | std::ios::binary);
	std::locale::global(loc);
	if (!ofs.is_open())
		return false;
	ofs.write(content.c_str(), sizeof(char) * content.length());
	ofs.flush();
	ofs.close();
	return true;
}

unsigned long long FileUtil::GetDiskAvailableSpace(std::string dir)
{
	// 磁盘允许存储空间
	boost::filesystem::path pt(dir);
	boost::filesystem::space_info info = boost::filesystem::space(pt);
	return info.available;
}

unsigned long long FileUtil::GetDiskCapacitySpace(std::string dir)
{
	// 磁盘空间
	boost::filesystem::path pt(dir);
	boost::filesystem::space_info info = boost::filesystem::space(pt);
	return info.capacity;
}

unsigned long long FileUtil::GetDiskFreeSpace(std::string dir)
{
	// 磁盘空闲空间
	boost::filesystem::path pt(dir);
	boost::filesystem::space_info info = boost::filesystem::space(pt);
	return info.free;
}